package com.example.juccode.juc01threadpool.demo04;

import com.example.juccode.juc01threadpool.demo04.Task.CompletionTask;

import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @description: TODO
 * @date 2025/11/4 17:21
 */
public class CompletionDemo {

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);

        ExecutorCompletionService<Integer> completionService = new ExecutorCompletionService<>(threadPool);

        for (int i = 5; i >= 1; i--) {
            CompletionTask task = new CompletionTask(i);
            completionService.submit(task);
        }
        try {
            for (int i = 0; i < 5; i++) {
                Integer result = completionService.take().get();
                System.out.println("结果：" + result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭线程池
            threadPool.shutdown();
        }

    }
}
